package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.event.model.DriverGoPickUpEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsTempletService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverGoPickUpEventListener implements ApplicationListener<DriverGoPickUpEvent> {

    private static final Logger logger = LoggerFactory.getLogger(DriverGoPickUpEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;


    @Override
    @Async
    public void onApplicationEvent(DriverGoPickUpEvent event) {

        logger.debug("driver gopickup event:{}", "");

        Driver driver = (Driver) event.getSource();
        Driver picker = event.getPicker();

        if (driver.getStatus() == Driver.DRIVER_ONLINE) {
            //web推送
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("driverID", driver.getId());
            jsonObject.put("type", "gopickup");
            Company mainCompany = companyService.getMain();
            Company driverCompany = companyService.findOne(driver.getCompanyId());
            WebSocketSend
                .boardcastToArea(jsonObject, driverCompany.getCityName(), mainCompany.getId());
        }

        try {

            PushDataV4 pushDataV4 = new PushDataV4();
            pushDataV4.setCode("0X10");
            Map<String, String> extra = new HashMap<String, String>();
            extra
                .put("content", "工作车" + picker.getRealname() + "已接受您的请求，联系电话:" + picker.getPhone());
            pushDataV4.setExtra(extra);
            pushDataV4.setType("0X01");

            boolean getuiOnline = false;
            if (StringUtils.isNotBlank(driver.getUserID())) {
                getuiOnline = GeTuiEmployV4.getClientOnline(driver.getUserID(), settingUtils.get());
            }

            if (getuiOnline) {
                GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
            } else {
                if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                    JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                }
            }

        } catch (IOException e) {
            logger.debug("push error : " + e.getMessage());
        }

    }

}
